<?php
/*
 *   This file is part of poat.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 */

class task {

	var $id;
	var $teamid;
	var $team;
	var $startdate;
	var $enddate;
	var $task;
	
    public function __construct()
    {
    }

	// THIS CREATE A NEW INSTANCE OF THIS CLASS
	public function newInstance() {
		//$class = new ReflectionClass('task');	
		//return $class->newInstance();
		return new task();
	}

	// COMMON PUBLIC FUNCTIONS // NEEDED FOR LOADER
	public function get() {
		$sql = "SELECT * FROM ".TASK." WHERE id = ".util::str($this->id);
		return $sql;
	}

	public function delete() {
		$sql = "DELETE FROM ".TASK." WHERE id = ".util::str($this->id);
		return $sql;
	}
	
	public function listAll() {
		$sql = "SELECT * FROM ".TASK." ORDER BY startdate ASC";
		return $sql;
	}

	public function listActiveTask() {
		$sql = "SELECT * FROM ".TASK;
		$sql .= " WHERE (teamid =".$this->teamid." OR teamid = '0') AND startdate<=".util::str(util::now())." AND enddate>=".util::str(util::now())." ORDER BY enddate ASC";
		return $sql;
	}

	public function listBeginTasks() {
		$today = date('Y-m-d',time());
		/*
		$sql = "SELECT * FROM ".TASK;
		$sql .= " WHERE (teamid =".$this->teamid." OR teamid = '0') " .
				"AND YEAR(startdate)=".util::str($year).
				" AND MONTH(startdate)=".util::str($month).
				" AND DAY(startdate)=".util::str($day). " AND status = '0'";
				*/
		$sql = "SELECT * FROM ".TASK;
		$sql .= " WHERE DATE(startdate)<=".util::str($today)." AND status = '0'";
		return $sql;
	}

	public function listEndTasks() {
		$today = date('Y-m-d',time());
		$sql = "SELECT * FROM ".TASK;
		$sql .= " WHERE DATE(enddate)<=".util::str($today)." AND (status = '0' OR status = '1') ";
		return $sql;
	}
		
	public function listByTeam() {
		$sql = "SELECT * FROM ".TASK;
		$sql .= " WHERE teamid =".$this->teamid." OR teamid = '0' ORDER BY startdate ASC";
		return $sql;
	}
		
	public function insert() {
      $sql = "INSERT INTO ".TASK." (teamid, startdate, enddate, task) ";
      $sql .= "VALUES (";
      $sql .= util::str($this->teamid);
	  $sql .= ",".util::str($this->startdate);
	  $sql .= ",".util::str($this->enddate);
	  $sql .= ",".util::str(util::encode($this->task));
	  $sql .= ")";
	  return $sql;
	}

	public function update() {
      $sql = "UPDATE ".TASK;
      $sql .= " set teamid = ".util::str($this->teamid);
      $sql .= ", startdate = ".util::str($this->startdate);
      $sql .= ", enddate = ".util::str($this->enddate);
      $sql .= ", task = ".util::str(util::encode($this->task));
      $sql .= " WHERE id = ".util::str($this->id);
	  return $sql;
	}
	
	public function setStatus($status) {
      $sql = "UPDATE ".TASK;
      $sql .= " set status = ".util::str($status);
      $sql .= " WHERE id = ".util::str($this->id);
      return $sql; 	
	}

	public function fill($array) {
		$this->id=$array['id'];
		$this->teamid=$array['teamid'];
		$this->task=util::decode($array['task']);
		$this->startdate=$array['startdate'];
		$this->enddate=$array['enddate'];
	}
	
	public function startdatef() {
		return date('Y-m-d',strtotime($this->startdate));
		//return util::fromUTC($this->startdate,$zone);
	}

	public function enddatef() {
		return date('Y-m-d',strtotime($this->enddate));
		//return util::fromUTC($this->enddate,$zone);
	}
	
	public function status() {
		
		$start = strtotime($this->startdate);
		$end = strtotime($this->enddate);

		if ($start>time()) {
			return TASK_PENDING;
		} elseif ($start<=time() and $end>=time()) {
			return TASK_STARTED;
		} else {
			return TASK_FINISHED;
		}
	}
}

?>
